import java.util.HashSet;
import java.util.LinkedList;

import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=94 lang=java
 *
 * [94] 二叉树的中序遍历
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    LinkedList<TreeNode> data=new LinkedList<>();
    LinkedList<Integer> res=new LinkedList<>();
    HashSet<TreeNode> flag=new HashSet<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        data.addLast(root);
        while(!data.isEmpty())
        {
            TreeNode node=data.pollFirst();
            if (flag.contains(node)) {
                res.add(node.val);
                continue;
            }
            if (node!=null) {
                data.addFirst(node.right);
                data.addFirst(node);
                data.addFirst(node.left);
                flag.add(node);
                
            }
        }
        return res;
    }
}
// @lc code=end

